package com.aaa.interceptor;

import com.aaa.entity.SysLog;
import com.aaa.mapper.SysLogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

@Component
public class RequestLoggingInterceptor implements HandlerInterceptor {
    @Resource
    private SysLogMapper sysLogMapper;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {

        String requestUrl = request.getRequestURI();
        String requestIp = request.getRemoteAddr();
        String requestMethod = request.getMethod();
        if (!requestUrl.contains("/monitor/server")){
            SysLog sysLog = new SysLog();
            sysLog.setReqIp(requestIp);
            sysLog.setReqMethod(requestMethod);
            sysLog.setReqUrl(requestUrl);
            sysLog.setReqDate(new Date());

            sysLogMapper.addLog(sysLog);
        }

        return true;
    }
}
